Linux image in Docker?

Asked

Viewed 204 times

4

I’m trying to understand the concept of Docker but it’s still a little confusing for me. Docker would be an image generator/emulator that can be managed in the cloud? If so, I believe I have understood.

But the point is, if this is really it, I don’t understand how it works. How do I pull an image and rotate it in Docker?

1 answer

5


There are two ways to create custom images with commit and with dockerfile.

Commit:

We first need to create some container:

docker run -it --name containername ubuntu:16.04 bash

Now that we are in the container bash, we will install Nginx in it:

apt-get update
apt-get install nginx -y
exit

Let’s stop the container with the command below:

docker stop containername

Now let’s commit this container to an image:

docker commit containername meuubuntu:nginx

containername is the name of the container we created and modified in the previous steps and the name meuubuntu:nginx is the resulting image of the commit, i.e., the state of the containername will be stored in an image called meuubuntu:nginx that in that case the only change we have of the official image of the Ubuntu in version 16.04 is a package nginx installed.

To view the image list and find the one you just created, run the command below again:

docker images

To test your new image, let’s create a container from it and check whether the nginx is installed:

docker run -it --rm meuubuntu:nginx dpkg -l nginx

If you want to validate, you can run the same command in the official image of Ubuntu:

docker run -it --rm ubuntu:16.04 dpkg -l nginx

It is worth noting that the commit method is not the best option to create images, because as we could verify, the image modification process is completely manual and presents some difficulty to track the changes that have been made, since what has been manually modified is not automatically registered in the Docker structure.

Dockerfile

First create any file for a future test:

touch arquivo_teste

Create a file called Dockerfile and within it put the following content:

FROM ubuntu:16.04
RUN apt-get update && apt-get install nginx -y
COPY arquivo_teste /tmp/arquivo_teste
CMD bash

In the above file I used four instructions:

FROM is used to inform which image was used as a basis, in which case Ubuntu 16.04.

RUN is used to inform which commands will be executed in that environment to effect the necessary changes to the system infrastructure. They are like commands executed in the shell of the environment, same as the model by commit, but in this case it was done automatically and completely traceable, since this Dockerfile will be stored in the version control system.

COPY is used to copy files from the station where you are running the build into the image. We use a test file only to exemplify this possibility, but this instruction is often used to send environment configuration files and codes to run on application services.

CMD is used to tell which command will be executed by default, if none is informed at the container startup from that image. In our case we put the bash command, that is, if this image is used to start a container and we do not inform the command, it will execute the bash.

After building your Dockerfile just run the command below:

docker build -t meuubuntu:nginx_auto .

The above command has the option -t which serves to inform the name of the image that will be created, which in our case meuubuntu:nginx_auto and the . at the end informs which context should be used in this image construction, that is, all files in your current folder will be sent to the service of the Docker and only they can be used for manipulations of the Dockerfile (Example of the use of COPY).

The result of each instruction of this file is stored in a local cache, that is, if the Dockerfile is not modified in the next image creation (build) the process will not take long, because everything will be in the cache, but if something is modified only the modified instruction and later will be executed again.

The suggestion to take advantage of the Dockerfile cache is to always keep instructions that change more frequently closer to the document base. Remember to also meet the dependencies between instructions.

Let’s use an example to make it clearer:

FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install nginx
RUN apt-get install php5
COPY arquivo_teste /tmp/arquivo_teste
CMD bash

If we modify the third line of the file and instead of installing the nginx move to apache2, the instruction that makes the update in the apt will not be run again, but the installation of the apache2 will be installed as it has just entered the file, as well as the php5 and copy the file, as they are all subsequent to the modified line.

Take a good look at the website of Docker have a lot of information you’re looking for or here, source: Techfree

Browser other questions tagged

You are not signed in. Login or sign up in order to post.