Linux image in Docker?

4

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

But the point is, if it's really that, I do not understand how it works. How do I pull an image and run it in Docker ?

    
asked by anonymous 06.09.2017 / 16:28

1 answer

4

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

  

Commit:

We need to create a container first:

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

Now that we're in the bash of the container, let's install nginx on 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 in an image:

docker commit containername meuubuntu:nginx

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

To view the list of images and find the one you just created, re-run the command below:

docker images

To test your new image, we'll create a container from it and check that 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 ubuntu image:

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 trace the changes that were made, since what was done manually modified is not automatically registered in the docker framework.

  

DockerFile

First create any file for a future test:

touch arquivo_teste

Create a file named Dockerfile and inside 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 tell which image is used as the base, in this case it was Ubuntu 16.04 .

RUN is used to inform which commands will run in this environment to make the necessary changes to the system infrastructure. They are like commands executed in the environment shell, just like the commit model, 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 workstation where you are building the image into the image. We use a test file just to exemplify this possibility, but this statement is widely used to send environment configuration files and codes to run in application services.

CMD is used to indicate which command will be executed by default, if none is informed when a container is started from this 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 bash.

After building your Dockerfile simply run the command below:

docker build -t meuubuntu:nginx_auto .

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

The result of each instruction in this file is stored in a local cache, ie, if Dockerfile is not modified at next build, the process will not be slow, cache, but if something is modified only the modified statement and later it will be executed again.

The suggestion to take advantage of the Dockerfile cache is to always keep instructions that change more often nearer the base of the document. You should also remember the dependencies between statements.

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 nginx we change to apache2 , the statement that does update in apt will not run again, but the apache2 installation will be installed, since you just entered the file, as well as the php5 and the file copy, because all of them are subsequent to the modified line.

  

Take a look at the Docker site have lots of information you are looking for, or < a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-use-do-dock-first-passages"> here , source: TechFree

    
06.09.2017 / 16:54