Here is a list of basic commands with examples:
Basic Docker Commands
- List Local Images
- Download an image
- List Docker processes
- List containers
- Inspect all configuration of a container
- Rename container
- Starting and stopping containers
- Check container ports
- View container logs
- Enter a container through shell
- Copy or put files inside a container
- Deleting containers and volumes
- Create a network
List Local Images
Download an image
List Docker processes
List containers
To list all containers on the host
Inspect all configuration of a container
This returns network info, mounted volume, etc…
Rename container
Rename a docker container
now you can doAssign 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)
$ 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:
-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
If we start without -d, the shell stays stopped and we can kill the process with Control+C when we want.
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= optiondocker run --rm -it -d --name danipenaperez_mkdocs -p 9999:8000 -v $PWD:/docs danipenaperez/mkdocs:latest
This way we can stop the container
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
Check container ports
Check the ports exposed by a container
View container logs
View the logs of a container
View continuous output of container logs
Enter a container through shell
ENTER A STOPPED CONTAINER (THAT CANNOT START FOR EXAMPLE) To enter a container that cannot startVOLUMES
Copy or put files inside a container
Copy files into/out container, not necessary running container
In DockerCompose volumes are shared in
Deleting containers and volumes
The simple way is like this
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
Delete a container that is running (-force)
Delete all stopped containers (whether referenced or not)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
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
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/