## docker run
# [-d] run the container in detached mode, aka in the background
# [-p 80:80] map port 80 of the host to port 80 in the container
docker run -d -p 80:80 docker/getting-started
## docker build
# [-t] tag the image, a human-readable name for the final image
# [.] path to look for Dockerfile
docker build -t getting-started .
## remove a container
# get the ID of the container
docker ps
# stop the container
docker stop <the-container-id>
# remove the container
docker rm <the-container-id>
# stop and remove the container
docker rm -f <the-container-id>
## share via docker hub
# login into docker hub
docker login -u YOUR-USER-NAME
# tag the image with a proper name
docker tag getting-started YOUR-USER-NAME/getting-started
# push to docker hub with the default latest tag
docker push YOUR-USER-NAME/getting-started
## data persistance
# run ubuntu container
# start bash and run two commands
docker run -d ubuntu bash -c "shuf -i 1-10000 -n 1 -o /data.txt && tail -f /dev/null"
# get into the running container
docker exec <container-id> cat /data.txt
# run another ubuntu container
# run ls command
docker run -it ubuntu ls /
## create a named volume - just to store data
docker volume create todo-db
# start app with an attached named volume
# [-v] map host volume to container path
docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
# inspect details of a named volume
# [Mountpoint] the actual place where data is stored
# [driver] many 3rd party drivers available to support various storage options
docker volume inspect todo-db
## bind mount - we control the exact mountpoint on the host
# [-w /app] set the working dir that the command will run from
# [-v "$(pwd):/app"] bind mount current dir from host in the container into the /app dir
docker run -dp 3000:3000 -w /app -v "$(pwd):/app" node:12-alpine sh -c "yarn install && yarn run dev"
## inspect logs
docker logs -f <container-id>
## container networking
# If two containers are on the same network, they can talk to each other. If they aren't, they can't.
# 1) Assign it at start or 2) connect an existing container
# create a network
docker network create todo-app
# start a MySQL container and attach it the network
# [--network todo-app] create a network
# [--network-alias mysql] set network alias/hostname for connection from other containers
# [-v todo-mysql-data:/var/lib/mysql] create a volume automatically
# [-e MYSQL_ROOT_PASSWORD=secret] set env var for container
docker run -d --network todo-app --network-alias mysql -v todo-mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=todos mysql:5.7
# connect to container
docker exec -it <mysql-container-id> mysql -p
# start new container - nicolaka/netshoot, for network debugging
docker run -it --network todo-app nicolaka/netshoot
# look up the IP address for hostname mysql
dig mysql
# connect to mysql container for dev work
docker run -dp 3000:3000 -w /app -v "$(pwd):/app" --network todo-app -e MYSQL_HOST=mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=secret -e MYSQL_DB=todos node:12-alpine sh -c "yarn install && yarn run dev"
# verify status of the container
docker logs <container-id>
# confirm data is written into the database
docker exec -ti <mysql-container-id> mysql -p todos
## docker compose
# start multi container app
docker-compose up -d
# shut down
docker-compose down
# inspect logs
docker-compose logs -f <service_name>
## image layers
# inspect layers that made up an image
# base layer shows at bottom
docker image history --no-trunc getting-started
# layer caching
# once a layer changes, all downstream layers have to be recreated as well
# place common image close to base image
# .dockerignore file can be used to skip copy of folders/files
## multi-stage build - in Dockerfile
# FROM maven AS build
# ...
# FROM tomcat
# COPY --from=build /app/target/file.war /usr/local/tomcat/webapps
Created
November 4, 2020 11:36
-
-
Save bitsurgeon/53c56406965ff8247e16e30a1165583d to your computer and use it in GitHub Desktop.
frequent docker operations
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment