$ docker
$ docker version
$ docker info
$ docker container run -it -p 80:80 nginx
$ docker container run -d -p 80:80 nginx
$ docker container run -d -p 80:80 nginx
$ docker container run -d -p 80:80 --name nginx-server nginx
- Looked for image called nginx in image cache
- If not found in 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"
$ docker container ls
OR
$ docker ps
$ docker container ls -a
$ docker container stop [ID]
$ docker stop $(docker ps -aq)
$ docker container rm [ID]
$ docker container rm -f [ID]
$ docker container rm [ID] [ID] [ID]
$ docker rm $(docker ps -aq)
$ docker container logs [NAME]
$ docker container top [NAME]
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 youll need to connect directly to that. On Linux however you can run "ps aux" and see the processes directly
$ docker image ls
$ docker pull [IMAGE]
$ docker image rm [IMAGE]
$ docker rmi $(docker images -a -q)
- Images are app bianaries and dependencies with meta data about the image data and how to run the image
- Images are no a complete OS. No kernel, kernel modules (drivers)
- Host provides the kernel, big difference between VM
NGINX:
$ docker container run -d -p 80:80 --name nginx nginx (-p 80:80 is optional as it runs on 80 by default)
APACHE:
$ docker container run -d -p 8080:80 --name apache httpd
MONGODB:
$ docker container run -d -p 27017:27017 --name mongo mongo
MYSQL:
$ docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql
$ docker container inspect [NAME]
$ docker container inspect --format '{{ .NetworkSettings.IPAddress }}' [NAME]
$ docker container stats [NAME]
$ docker container run -it --name [NAME] nginx bash
- i = interactive Keep STDIN open if not attached
- t = tty - Open prompt
For Git Bash, use "winpty"
$ winpty docker container run -it --name [NAME] nginx bash
$ docker container run -it --name ubuntu ubuntu
(no bash because ubuntu uses bash by default)
$ docker container run --rm -it --name [NAME] ubuntu
$ docker container start -ai ubuntu
$ docker container exec -it mysql bash
$ docker container run -it alpine sh
(use sh because it does not include bash) (alpine uses apk for its package manager - can install bash if you want)
$ docker container port [NAME]
$ docker network ls
$ docker network inspect [NETWORK_NAME]
("bridge" is default)
$ docker network create [NETWORK_NAME]
$ docker container run -d --name [NAME] --network [NETWORK_NAME] nginx
$ docker network connect [NETWORK_NAME] [CONTAINER_NAME]
$ docker network disconnect [NETWORK_NAME] [CONTAINER_NAME]
$ docker network disconnect
$ docker image ls
Youll see that each image has a tag
$ docker image tag nginx btraversy/nginx
$ docker image push bradtraversy/nginx
$ docker login
$ docker image tag bradtraversy/nginx bradtraversy/nginx:testing
- 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 image
- WORKDIR - Sets working directory (also could use 'RUN cd /some/path')
- COPY # Copies files from host to container
$ docker image build -t [REPONAME] .
- If you re-run the build, it will be quick because everythging 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
FROM nginx:latest # Extends nginx so everything included in that image is included here
WORKDIR /usr/share/nginx/html
COPY index.html index.html
$ docker image build -t nginx-website
$ docker container run -p 80:80 --rm nginx-website
$ docker image tag nginx-website:latest btraversy/nginx-website:latest
$ docker image push bradtraversy/nginx-website
$ docker volume ls
$ docker volume prune
$ docker pull mysql
$ docker image inspect mysql
$ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True mysql
$ docker container inspect mysql
- You will also see the volume under mounts
- Container gets its own uniqe location on the host to store that data
- Source: xxx is where it lives on the host
$ docker volume ls
There is no way to tell volumes apart for instance with 2 mysql containers, so we used named volumes
$ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql
docker volume inspect mysql-db
- 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 local path, for working directory use $(pwd):/path/container - On windows may not work unless you are in your users folder
$ docker container run -p 80:80 -v $(pwd):/usr/share/nginx/html nginx
$ docker container exec -it nginx bash
$ cd /usr/share/nginx/html
$ ls -al
$ touch test.txt
- 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)
- containers
- networks
- volumes
version: '2'
# same as
# docker run -p 80:4000 -v $(pwd):/site bretfisher/jekyll-serve
services:
jekyll:
image: bretfisher/jekyll-serve
volumes:
- .:/site
ports:
- '80:4000'
docker-compose up
docker-compose up -d
docker-compose down
Updated Version
Docker Commands, Help & Tips
Show commands & management commands
Docker version info
Show info like number of containers, etc
WORKING WITH CONTAINERS
Create an run a container in foreground
Create an run a container in background
Shorthand
Naming Containers
TIP: WHAT RUN DID
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 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 youll 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
Some sample container creation
NGINX:
APACHE:
MONGODB:
MYSQL:
CONTAINER INFO
View info on container
Specific property (--format)
$ docker container inspect --format '{{ .NetworkSettings.IPAddress }}' [NAME]
Performance stats (cpu, mem, network, disk, etc)
ACCESSING CONTAINERS
Create new nginx container and bash into
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 network
or
Link to container, add to network
Create container on network
Connect existing container to network
Disconnect container from network
Detach network from container
Delete/Remove network
To remove the network by name or id, multiple can be deleted:
IMAGE TAGGING & PUSHING TO DOCKERHUB
tags are labels that point ot an image ID
Youll see that each image has a tag
Retag existing image
Upload to dockerhub
If denied, do
Add tag to new image
DOCKERFILE PARTS
Build image from dockerfile (reponame can be whatever)
From the same directory as Dockerfile
Benchmarking builds
TIP: CACHE & ORDER
EXTENDING DOCKERFILE
Custom Dockerfile for html paqge with nginx
Build image from Dockerfile
Running it
Tag and push to Dockerhub
VOLUMES
Volume - Makes 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 container
TIP: Mounts
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 new named volume
BIND MOUNTS
TIP: Instead of typing out 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 exiost on the host as well
Link
now Link it with dot net
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)
Example 2
To run
You can run in background with
To cleanup