Running containers
Last updated
Last updated
Running images with Docker can be done using the Docker run
command. The command can take many attributes that give more information on how to run the container.
To run the hello-world image that we created earlier, we can use the following command.
The command creates a container based on the hello-world image. Next we need to instruct Docker that we also want the internal port 80 to be available on port 80 on the host machine. This can be achieved by using the -p
flag. All traffic to port 80 of the Docker host will be forwarded to port 80 in the container.
The -it
flag will make sure the output of the container is forwarded to our terminal. Terminating the terminal output will also result in the termination of the container.
Other flags are documented in the Docker documentation:
Lets execute the command and check if everything is running.
The terminal will output the output of the container when running this command. It should look something like this:
This is the normal output of the Apache application.
If all goes well, we should be able to see the webpage.
In the terminal we should also see the logging information of Apache detecting our HTTP request and showing some information about that request:
To end the Docker container, press CTRL+C
. The container will then stop. The Apache server will stop, and it won't be possible to access the webpage again.
The docker run
command that we used allows only a single container to run. This might be good to test something quickly, but on a production environment, many containers will be running.
Docker makes it possible to run containers in the background. This can easily be realized by providing the -d
flag with the docker run
command. The -d
flag will deamonize the container. This means that the container will be running in the background.
When running this command, the terminal will return a random string that represents the newly created container. This output can be used to automate other tasks.
It is now possible to execute other commands in the terminal. The container will keep running until it is stopped manually.
Test this by surfing to the webpage again and checking if you get the Hello World message back.
To list which containers are running, you can use the following command:
This will show something like this:
Stopping containers can be done with the following command:
To stop the container that we created earlier, we can run the following command using its CONTAINER_ID
:
It is also possible to stop the container using its NAME. In this case the command should look like this:
Note that docker will give all its containers a random name. It is possible to assign a custom name to a container by providing the --name
flag with the docker run
command.
Lets start up a browser and surf to .