12.05.15



CREATING DOCKER CONTAINERS





THERE ARE 2 WAYS TO CREATE DOCKER CONTAINERS

1.) COMMAND LINE

2.)DOCKER FILE



COMMAND LINE



The docker run command is the command used to launch Docker containers.

The first time you run a software image, the docker command looks for it on your local system.

If the image isn’t on your local system docker will get it from the docker hub.

Let's take a close look at the 2 variationS listed below:


1.) docker run -it centos

2.) docker run-dit centos




docker run -it centos - (temporary)



docker run -it centos - (temporary)

Creates a docker container with Centos installed temporarily

If you exit and come back the container will be stopped

You would need to restart the container

- it is for an interactive session

t is for tty

A tty is a text input output environment (shell). The -it flag gives you an interactive tty to the docker container. It is as if you are inside the shell for the docker container. The stdout for the docker container is piped to your current shell and your input is piped to the docker container.

In linux when you run a command, you need a terminal (tty) to execute it.

When you want to connect to docker or run a command in a docker container you have to provide the option -t provides the terminal inside of the docker container.





The docker container 225e6d21000e was created.

Now let's exit and see what happens when we re-run the docker ps command.





As we can see the docker container 225e6d21000e no longer displays.



docker run -dit centos -(detached mode)



docker run -dit centos - (detached mode)

Creates a docker container with Centos installed permanently

Now lets create a container in detached mode

-d runs docker in detached mode.

Detached mode will create a permanent container.

i is for interactive mode.

t is for tty





The docker container c4889de7c3bc is created.

Let's exit and run the docker ps command again.





Docker container c4889de7c3bc is still there.


Reminder:

For both Temporary and Detached modes, when you run the command it will look for the images locally first in the docker host.

If the image is not found locally it will download the image from the docker hub.



Dockerfile





The second way of creating a docker container is by using a Dockerfile.

A Dockerfile is nothing more than a text file with instructions on how to build an image. It's an automation of the docker image creation.

Let's start:

Step 1 - Create a directory and call it whatever you like. For this example I will name the directory dockerfiles by typing mkdir dockerfiles.

Step 2 - Change to the directory dockerfiles - cd dockerfiles

Step 4 - Create a file Called Dockerfile and start editing it.


The file syntax will look like this:


FROM centos

# gets a base image from centos


RUN yum update -y

# installs updates


RUN yum install httpd -y

# installs Apache





Step 5 - Build an image

run the command: docker build .

docker build followed by the location of your Dockerfile

. represents the current working directory.

You can see that all 3 steps were run and an image id of d10aa382f33c was built successfully.





To view docker images run docker images. You can see the image id d10aa382f33c we just built.





Step 6 - Run the image docker run -dit d10aa382f33c.

This will create a container.





A container called 4fca8402d798 has now been created from the image called d10aa382f33c.