We will learn how to run containers in docker. This is the first and first and foremost command that we use in docker to run a containerized application . This section will cover basics of docker run using CLI as well as GUI so that everyone can follow along .
Just remember , at any point of time we can get help for this section by using the following command.
docker container help
The following commands are used to manage the life cycle of a container .
- create
- run
- start
- stop
- kill
- ls
- pause
- unpause
1. Create
This is the first step in the container life cycle , that is container getting created.

# docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]
docker container create nginx
As soon as you run the command , docker daemon will first look for the image locally , if it does not find it in the local image cache , it will pull the image from docker hub.
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
33847f680f63: Pulling fs layer
dbb907d5159d: Pulling fs layer
8a268f30c42a: Pulling fs layer
b10cf527a02d: Pulling fs layer
c90b090c213b: Pulling fs layer
1f41b2f2bf94: Pulling fs layer
b10cf527a02d: Waiting
c90b090c213b: Waiting
1f41b2f2bf94: Waiting
8a268f30c42a: Verifying Checksum
8a268f30c42a: Download complete
b10cf527a02d: Verifying Checksum
b10cf527a02d: Download complete
c90b090c213b: Verifying Checksum
c90b090c213b: Download complete
33847f680f63: Verifying Checksum
33847f680f63: Download complete
1f41b2f2bf94: Verifying Checksum
1f41b2f2bf94: Download complete
dbb907d5159d: Verifying Checksum
dbb907d5159d: Download complete
33847f680f63: Pull complete
dbb907d5159d: Pull complete
8a268f30c42a: Pull complete
b10cf527a02d: Pull complete
c90b090c213b: Pull complete
1f41b2f2bf94: Pull complete
Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90
Status: Downloaded newer image for nginx:latest
Once the image is pulled ,it gets created . To see the status of the containers we will use the docker container list command which will show us the status .

2 . Start
Now let us start the container by using the container start command
docker container start vibrant_kowalevski


As you can see from the output of the status command , the container was create a few minutes ago but started just 4 seconds ago.
3. Run
There is one more way we can start the container that is using the run command . This command does the job of creating the container as well as running it .


One important observation when we use run command , the shell from where you run the command will be taken over by the command that is running with in the container. Meaning , if you want your prompt back we have to kill it by using ctrl +C . This will kill the container process and push it to stopped state .

3. Stopping container
Gracefully
If we want to stop a running container gracefully , we have to use docker container stop command. This is like doing a shutdown before turning off the power switch .

docker container stop [OPTIONS] CONTAINER [CONTAINER...]
docker container stop vibrant_kowalevski

This command will wait for 10 seconds before killing the process forcefully , at times containerized application will be processing something and cannot be terminated immediately we can specify time out using flag –time <numberofseconds>this will wait for specified number of seconds before killing the process forcefully .
Forcefully
At times when we want to stop the container without waiting for the application to stop gracefully.

docker container kill [OPTIONS] CONTAINER [CONTAINER...]
docker container stop vibrant_kowalevski

3. Restart container
At times we might have to restart the container for any number of reasons . We can do it by way of stopping the container and starting it again or we can simply use the restart command.

docker container restart [OPTIONS] CONTAINER [CONTAINER...]
docker container restart vibrant_kowalevski
It also has the timeout option similar to docker container stop
4. Pausing/resuming container
Docker container pauses process with in the provided container/s , using start command we can resume the paused processes.
docker container pause [OPTIONS] CONTAINER [CONTAINER...]
docker container pause vibrant_kowalevski
docker container unpause vibrant_kowalevski

5. Remove
This does exactly opposite of create, ie removes container. Container can be remove if they are in stopped state or we can force remove any container by using –force flag.

We will be deep diving into some of these commands in the future posts
References :
https://docs.docker.com/


