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
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:
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)
