Docker Commands

Docker Commands, Help & Tips

Show commands & management commands:

$ docker

Docker version info:

$ docker version

Show info like the number of containers:

$ docker info

WORKING WITH CONTAINERS

Create an run a container in the foreground:

$ docker container run -it -p 80:80 nginx

Create an run a container in the background:

$ docker container run -d -p 80:80 nginx

Shorthand:

$ docker container run -d -p 80:80 nginx

Naming Containers:

$ docker container run -d -p 80:80 --name nginx-server nginx

TIP: WHAT RUN DID

  • Looked for an image called Nginx in the image cache

  • If not found in the cache, it looks to the default image repo on Dockerhub

  • Pulled it down (latest version), stored in the image cache

  • Started it in a new container

  • We specified to take port 80- on the host and forward to port 80 on the container

  • We could do "$ docker container run --publish 8000:80 --detach nginx" to use port 8000

  • We can specify versions like "nginx:1.09"

List running containers

OR

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

TIP: ABOUT CONTAINERS

Docker containers are often compared to virtual machines but they are actually just processes running on your host os. In Windows/Mac, Docker runs in a mini-VM so to see the processes you'll need to connect directly to that. On Linux however, you can run "ps aux" and see the processes directly

IMAGE COMMANDS:

List the images we have pulled

We can also just pull down images

Remove image

Remove all images

TIP: ABOUT IMAGES

  • Images are app binaries and dependencies with metadata about the image data and how to run the image

  • Images are no complete OS. No kernel, kernel modules (drivers)

  • The host provides the kernel, big difference between VM

Some sample container creation

NGINX:

APACHE:

MONGODB:

MYSQL:

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

  • i = interactive Keep STDIN open if not attached

  • t = tty - Open prompt

For Git Bash, use "winpty"

Run/Create Ubuntu container

(no bash because ubuntu uses bash by default)

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

(use sh because it does not include bash) (alpine uses APK for its package manager - can install bash if you want)

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

Check volumes

Cleanup unused volumes

Pull down MySql image to test

Inspect and see volume

Run container

Inspect and see volume in the container

TIP: Mounts

  • You will also see the volume under mounts

  • The Container gets its own unique location on the host to store that data

  • Source: xxx is where it lives on the host

Check volumes

There is no way to tell volumes apart for instance with 2 MySql containers, so we used named volumes

Named volumes (Add -v command)(the name here is MySql-DB which could be anything)

Inspect the new named volume

BIND MOUNTS

  • Can not use in Dockerfile, specified at run time (uses -v as well)

  • ... run -v /Users/brad/stuff:/path/container (mac/linux)

  • ... run -v //c/Users/brad/stuff:/path/container (windows)

TIP: Instead of typing out the local path, for working directory use $(pwd):/path/container - On windows may not work unless you are in your users folder

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

  • Configure relationships between containers

  • Save our docker container run settings in easy to read file

  • 2 Parts: YAML File (docker.compose.yml) + CLI tool (docker-compose)

1. docker.compose.yml - Describes solutions for

  • containers

  • networks

  • volumes

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

Youll see that each image has a tag

Retag existing image

Upload to docker hub

If denied, do

Add a tag to the new image

DOCKERFILE PARTS

  • FROM - The os used. Common is alpine, Debian, ubuntu

  • ENV - Environment variables

  • RUN - Run commands/shell scripts, etc

  • EXPOSE - Ports to expose

  • CMD - Final command run when you launch a new container from an image

  • WORKDIR - Sets working directory (also could use 'RUN cd /some/path')

  • COPY # Copies files from host to the container

Build an image from the dockerfile (repo name can be whatever)

From the same directory as Dockerfile

TIP: CACHE & ORDER

  • If you re-run the build, it will be quick because everything is cached.

  • If you change one line and re-run, that line and everything after will not be cached

  • Keep things that change the most toward the bottom of the 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:

Docker provides a single command that will clean up any resources — images, containers, volumes, and networks — that are dangling (not associated with a container):

circle-exclamation

Remove all images

Stop and remove all container

Start a shell in the container

Last updated