youhoc
Docker

icon picker
Container Basics

# version check
docker version
docker info
docker

# new way of running commands
docker container run (new)
docker run (still work)

Image vs Container

Image — the app we want to run, we build or others build
Container — an instance of that image running as as process
Can have many Containers running 1 Image
Docker Hub is Docker’s default image “registry” (like AppStore)

Commands

# run a container "nginx"
docker container run --publish 80:80 --detach --name webhost nginx

# it will return a container ID, use it to stop or remove container
6903032478...

# run in the background
-d / --detach

# open up port 80 on host, forward to port 80 in container
-p / --publish 80:80

# name a container
--name webhost

# list containers (running or all)
docker container ls
docker container ls -a
docker ps
docker ps -a

# run vs start: run always init new container, "start" an existing container
docker container start 690

# stop a container by ID (first 3 letters) or name
docker container stop 690
docker container stop webhost

# read logs of a container (by name of id)
docker container logs webhost

# read processes of a container
docker container top webhost

THIS IS IMPORTANT, A PROCESS RUN BY A CONTAINER IS A REAL PROCESS, JUST JUST A VM

# remove a container (use -f to force remove), multiple containers by space
docker container rm 690 abc xuz
docker container rm -f 690

# get help
docker container --help
What happens when a container run:
it looks for “nginx” image locally
look for remote image from Docker Hub
download latest version ( nginx:1.11 )
create new container
give container a virtual IP on a private network inside docker engine
open up port 80 on host, forward to port 80 in container
start container by using the CMD in the image Dockerfile
check the processes started by a container, you’ll see it is a real process ( ps aux | grep mongo )

Advanced Command

# create a mysql container named db with custom env var
docker container run -d -p 3306:3306 --name db --e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql

# check log to find password of mysql
docker container logs db

# check nginx on port 80
curl localhost

# check apache httpd on port 8080
curl localhost:8080

# list all images
docker image ls

# inspect how a container in JSON
docker container inspect

# show current stats of all container
docker container stats

# run shell inside a container
docker container run -it nginx bash
docker container run -it alpine sh
docker container start -ai
docker container exec -it webhost ping webhost2 # run ping inside webhost1 to ping to webhost 2

# when we run 'bash' after container boot-up, it will stop default start-up (container will not run)


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.