Docker Commands
Docker Commands, Help & Tips
Show commands & management commands:
$ dockerDocker version info:
$ docker versionShow info like the number of containers:
$ docker infoWORKING WITH CONTAINERS
Create an run a container in the foreground:
$ docker container run -it -p 80:80 nginxCreate an run a container in the background:
$ docker container run -d -p 80:80 nginxShorthand:
$ docker container run -d -p 80:80 nginxNaming Containers:
$ docker container run -d -p 80:80 --name nginx-server nginxTIP: WHAT RUN DID
List running containers
List all containers (Even if not running)
Stop container
Stop all running containers
Remove container (Can not remove running containers, must stop first)
To remove a running container use force(-f)
Remove multiple containers
Remove all containers
Get logs (Use name or ID)
List processes running in the container
IMAGE COMMANDS:
List the images we have pulled
We can also just pull down images
Remove image
Remove all images
Some sample container creation
CONTAINER INFO
View info on container
Specific property (--format)
Performance stats (CPU, Memory, Network, Disk, etc)
ACCESSING CONTAINERS
Create a new Nginx container and bash into
Run/Create Ubuntu container
You can also make it so when you exit the container does not stay by using the -rm flag
Access an already created container, start with -ai
Use exec to edit config, etc
Alpine is a very small Linux distro good for docker
NETWORKING
"bridge" or "docker0" is the default network
Get port
List networks
Inspect network
Create a network
Create a container on network
Connect existing container to the network
Disconnect container from the network
Detach network from container
VOLUMES:
Volume - Makes a special location outside of container UFS. Used for databases
Bind Mount -Link container path to host path
Check volumes
Cleanup unused volumes
Pull down MySql image to test
Inspect and see volume
Run container
Inspect and see volume in the container
Check volumes
Named volumes (Add -v command)(the name here is MySql-DB which could be anything)
Inspect the new named volume
BIND MOUNTS
Run and be able to edit index.html file (local dir should have the Dockerfile and the index.html)
Go into the container and check
You could create a file in the container and it will exist on the host as well
DOCKER COMPOSE
1. docker.compose.yml - Describes solutions for
2. docker-compose CLI - used for local dev/test automation with YAML files
Sample compose file (From Bret Fishers course)
To run
You can run in the background with
To cleanup
IMAGE TAGGING & PUSHING TO DOCKERHUB
Tags are labels that point to an image ID
Retag existing image
Upload to docker hub
If denied, do
Add a tag to the new image
DOCKERFILE PARTS
Build an image from the dockerfile (repo name can be whatever)
From the same directory as Dockerfile
EXTENDING DOCKERFILE
Custom Dockerfile for HTML page with Nginx
Build an image from Dockerfile
Running it
Tag and push to Dockerhub
Useful commands:
Remove all unused containers image:
Remove all images
Stop and remove all container
Start a shell in the container
Last updated