Skip to content

Here is a list of basic commands with examples:

Basic Docker Commands

  1. List Local Images
  2. Download an image
  3. List Docker processes
  4. List containers
  5. Inspect all configuration of a container
  6. Rename container
  7. Starting and stopping containers
  8. Check container ports
  9. View container logs
  10. Enter a container through shell
  11. Copy or put files inside a container
  12. Deleting containers and volumes
  13. Create a network

List Local Images

docker images

Download an image

docker pull %repository/idImage%

List Docker processes

docker ps -a (lists all processes)
docker ps (lists running processes)

List containers

To list all containers on the host

docker container ls --all

Inspect all configuration of a container

This returns network info, mounted volume, etc…

docker inspect idContainer

Rename container

Rename a docker container

docker rename idcontainer oracleFirstTest
now you can do
docker run oracleFirstTest

Assign name to container that will be created at startup with docker run

docker run -d -p 8080:8080 -p 1521:1521 --name oracleSeconf sath89/oracle-xe-11g  (this will call it oracleSeconf)
Verification:
$ docker ps
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS                                            NAMES
7af7d458bbdc        sath89/oracle-xe-11g   "/entrypoint.sh "   49 seconds ago      Up 47 seconds       0.0.0.0:1521->1521/tcp, 0.0.0.0:8080->8080/tcp   oracleSeconf

Starting and stopping containers

Start a container with an image:

docker run -options
@see https://docs.docker.com/engine/reference/commandline/exec/#options

-d makes it run in the background as a daemon -p exposes through the docker interface the container port dockerPort:internalContainerPort

In this example we start an oracle instance in daemon mode and we can access its port 1521 through the external container port 49161

docker run -d -p 49161:1521  wnameless/oracle-xe-11g

If we start without -d, the shell stays stopped and we can kill the process with Control+C when we want.

docker run --rm -it -p 9999:8000 -v $PWD:/docs danipenaperez/mkdocs:latest
If we need to do something and have it run in background we start with -d and we rely on the idea is that when it starts from the image, a name is established for the container to create using the –name= option
docker run --rm -it -d --name danipenaperez_mkdocs -p 9999:8000 -v $PWD:/docs danipenaperez/mkdocs:latest

This way we can stop the container

docker container ls -q --filter name=danipenaperez_mkdocs | docker container stop $(cat)

Specify volume path for the container, so that 2 containers can share the info in the same shared volume

docker run -d -p 8080:8080 -p 1521:1521 -v /my/local/shared/directory:/ContainerDirectory sath89/oracle-xe-11g

The simple way Stop a container

docker stop %idContainer%

Check container ports

Check the ports exposed by a container

docker port idContainer

View container logs

View the logs of a container

docker logs -f 23d0bc5a0f52

View continuous output of container logs

docker logs fdf3c2349621 -f

Enter a container through shell

docker exec -it <container name> /bin/bash
docker exec -it c8c5d402c49d /bin/bash
ENTER A STOPPED CONTAINER (THAT CANNOT START FOR EXAMPLE) To enter a container that cannot start
docker commit mysql57 mysql57_broken && docker run -it mysql57_broken /bin/bash

VOLUMES

Copy or put files inside a container

Copy files into/out container, not necessary running container

docker cp foo.txt mycontainer:/foo.txt
docker cp mycontainer:/foo.txt foo.txt

In DockerCompose volumes are shared in

Deleting containers and volumes

The simple way is like this

docker rm %idContainer%

If the container was using a volume it would not be deleted, to specify that it be deleted you have to Delete container and associated volumes

docker rm -v idContainer

Delete a container that is running (-force)

docker rm --force -v idContainer
Delete all stopped containers (whether referenced or not)
docker rm $(docker ps -a -q)

For Mass Operations:

docker-compose down [optional]

#Deleting all containers
docker rm -f $(docker ps -a -q)

#Deleting all volumes
docker volume rm $(docker volume ls -q)

docker-compose up -d [optional]

Extract a file from inside a container

docker cp <containerId>:/file/path/within/container ./

INCLUDE FOLDER IN DOCKER IMAGE (Dockerfile)

To add a directory inside an image, use ADD

FROM kong/kong-gateway:latest
USER root
# ADD local_machine_directory image_directory
ADD kong-plugin-jwt-auth-rbac/ /usr/local/share/lua/5.1/kong/plugins/jwt-auth-rbac/

DOCKER NETWORKS

Create a network

>docker network create --driver=bridge atlassian-network
>docker network list
Start instance using a certain network bridge and a name for the instance:

docker run -d -p 33061:3306 --net=atlassian-network --name mysql57 -e MYSQL_ROOT_PASSWORD=secret mysql:5.7 --character-set-server=utf8mb4

docker run --net=atlassian-network --name jira-software --detach --publish 8082:8080 cptactionhank/atlassian-jira-software:latest

DOCKER COMPOSE

VOLUMES

To define a volume, we must define it in the volumes section and then reference it in the service

version: '3'
services:
 minio:
  image: minio/minio:latest
  container_name: myminio
  ...
  volumes:
    - testbucketc:/tmp/testbucketc (Will look for testbucketc in the volumes section and if it's not there it will create it in /var/lib/docker/volumes/testbucketc)
    - ./source/:/tmp/source (this one instead references a relative directory of the host machine)
  ...
  ...
volumes:
  testbucketc:
    driver: local

Physical directories on the host machine will be at: /var/lib/docker/volumes/