Creating and Using Docker Containers

In this post i will give you the basic commands to run and troubleshoot basic docker containers

Install Docker First

This is a simple command to run a Nginx docker container:

docker container run --publish 844:80 --detach --name Mynginx nginx

  • — Detach = Run container in the background
  • — name = name of the container
  • nginx = image to run

What happens in ‘Docker container run’?

  • Looks for that image locally in the image cache,If it does not exist in the image cache he fetches it from Docker Hub Repo.
  • Creates new container based on the image (nginx: latest by default)
  • Gives it a virtual IP on a private network
  • opens up port 8080 on the host and forwards to port 80 in the container
  • Start container by using the CMD in the image Dockerfile
docker container ls
docker container ls -a
  • ls = show running containers
  • ls -a = show all containers in all status
docker container logs Mynginx
  • logs = show logs for the specific container (Mynginx)
[root@fedora ~]# docker container ls -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                 NAMES
b1311034dc4c   nginx     "/docker-entrypoint.…"   4 minutes ago    Created                                               Webhost2
6a936dd1a1ed   nginx     "/docker-entrypoint.…"   10 minutes ago   Up 10 minutes>80/tcp, :::844->80/tcp   Webhost
[root@fedora ~]# docker container rm -f b13 6a9
  • rm = Delete container
  • -f = force deletion of running container
  • b13,6a9 = containers ID’s

Whats going on inside a container

  • docker container top – show the process list in one container
root@fedora ~]# docker container top mysql
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
systemd+            56977               56956               0                   10:29               ?                   00:00:01            mysqld
  • docker container inspect – show details of one container configuration (Networking, mounts and more)
[root@fedora ~]# docker container inspect mysql 
        "Id": "5a1896ceb2cf076c64066183125e6b3814fb5e7109e392fde842230802837e31",
        "Created": "2021-08-18T07:29:01.167554924Z",
        "Path": "",
        "Args": [

  • Docker container stats = show performance stats for all containers
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O       BLOCK I/O         PIDS
f793d1abdadc   nginx     0.00%     9.875MiB / 31.03GiB   0.03%     16.9kB / 0B   7.04MB / 0B       9
5a1896ceb2cf   mysql     0.18%     443.1MiB / 31.03GiB   1.39%     27.9kB / 0B   38.5MB / 2.03GB   3

Getting a shell inside containers

  • docker container run -it = start new container interactively (if you exit the shell the container stopped)
[root@fedora ~]# docker container run --name meninginx -it nginx bash
root@64aa3ad9a53c:/# ls
bin		      etc    mnt   sbin  var
boot		      home   opt   srv
dev		      lib    proc  sys
docker-entrypoint.d   lib64  root  tmp  media  run   usr
root@64aa3ad9a53c:/# hostname

To re-run a stopped container and enter to his shell

root@fedora ~]# docker container ls -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                       PORTS                                                  NAMES
64aa3ad9a53c   nginx     "/docker-entrypoint.…"   4 minutes ago    Exited (130) 7 seconds ago                                                          meninginx
f793d1abdadc   nginx     "/docker-entrypoint.…"   40 minutes ago   Up 40 minutes                80/tcp                                                 nginx
5a1896ceb2cf   mysql     "docker-entrypoint.s…"   44 minutes ago   Up 44 minutes      >3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
[root@fedora ~]# docker container start -ai meninginx 
root@64aa3ad9a53c:/# ls
bin  boot  dev	docker-entrypoint.d  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@64aa3ad9a53c:/# ^C
  • docker container exec -it = open interactive shell to inside of the container
[root@fedora ~]# docker container exec -it mysql bash

Containers Resources

Check how much Resources container is using

root@master:~# docker stats nginx

To Limit container Memory

root@master:~# docker run -d --name nginx1 --memory "200mb"  nginx:alpine

root@master:~# docker stats nginx nginx1

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
8271c48d56c7        nginx               0.00%               4.055MiB / 3.817GiB   0.10%               1.01kB / 0B         12.2MB / 16.4kB     3
08fa208e8474        nginx1              0.00%               3.77MiB / 200MiB      1.88%               726B / 0B           0B / 16.4kB         3

To Limit container CPU

  • –cpuset-cpus 0,1 = You assign cpu 0 and cpu 1 (total 2 cpu)
  • –cpuset-cpus 0-2 = You assgin cpu from 0 to 2 (total of 3 cpu)
root@master:~# grep "model name" /proc/cpuinfo
model name      : Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
model name      : Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
root@master:~# grep "model name" /proc/cpuinfo | wc -l

root@master:~# docker run -d --name nginx2 --memory "300mb" --cpuset-cpus 0,1 nginx:alpine

Copy files from&to your container – docker cp

Copy Files from docker host to container

root@master:~# docker cp index.html nginx1:/usr/share/nginx/html/index.html

Copy Files from container to docker host

root@master:~# docker cp nginx1:/opt/test.txt .

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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