How to run a Docker Container in Detached Mode?

0
4166

When you are running your Docker containers, you can attach them to your current shell or run them as background tasks in your server. Detached mode means your shell is free, you can continue to type or run other things without affecting your Docker containers. Here we will run an nginx:alpine image in detached mode with -d option with the help of this command docker container run --name web -d -p 80:80 nginx:alpine

[root@server1 ~]# docker container run --name web -d -p 80:80 nginx:alpine
Unable to find image 'nginx:alpine' locally
alpine: Pulling from library/nginx
4167d3e14976: Pull complete
bb292c78f105: Pull complete
Digest: sha256:abe5ce652eb78d9c793df34453fddde12bb4d93d9fbf2c363d0992726e4d2cad
Status: Downloaded newer image for nginx:alpine
81f14281fc5b1cf9ff970837f9a37722da5cb310111c47687963e14404a04862
[root@server1 ~]#
[root@server1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                            NAMES
81f14281fc5b        nginx:alpine        "nginx -g 'daemon of…"   9 seconds ago       Up 7 seconds        0.0.0.0:80-                            >80/tcp   web
[root@server1 ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
81f14281fc5b        nginx:alpine        "nginx -g 'daemon of…"   4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp   web
[root@server1 ~]#

It is not detecting the image in the local repository cache of the Docker host where we are running this command. So it is pulling all the required layers from Docker Hub and running the container via using this image in detached mode.

And at the end, we are listing all running Docker containers with the help of docker ps command. The new syntax (recommended one) is docker container ls

Let us know if you could able to run your container in detached mode! Thanks.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.