A memorandum when using Docker and docker-compose.
The first site I learned a lot: Introduction to docker and docker-compose-Qiita
docker
commanddocker ps #List of running containers
docker ps -a #Running+List of finished containers
docker rm [ID] #Delete finished container
docker image ls #At hand(No need to download)Show docker image
docker rmi [Image name] #Delete docker image
docker run [Image name]
docker run --name [Label name] [Container name] #Label nameでコンテナを指定する
docker run -it --run [Container name] #Enter the container and exit to delete the container
docker run -d [Image name] /usr/sbin/httpd -DFOREGROUND #Start the httpd daemon
docker run -d -p 8080:80 [Image name] /usr/sbin/httpd -DFOREGROUND #Start the httpd daemon and skip container 80 to host 8080
docker run -v [Host PATH]:[PATH in container] #Specifying PATH between host and container
docker run --env TERM=xterm256color #Specifying environment variables
docker stop [ID] #Stop container
docker restart [ID] #While running a closed container
docker attach [ID] #Enter a running container
docker exec [ID] [command] #実行中のコンテナにcommandを与える
docker exec -it [ID] /bin/bash #Enter a running container
docker commit [Original image name when stopped] [Image name to make] #Make an image from the original image
When detaching, ctrl + p, q.
When using Dockerfile
Dockerfile
FROM centos:8
RUN yum update -y \
&& yum install httpd -y \
&& echo "hello world from dockerfile" > /var/www/html/index2.html \
&& yum clean all
ADD ./httpd/www/index.html /var/www/html/
EXPOSE 80
ENTRYPOINT ["/usr/sbin/httpd", "-DFOREGROUND"]
And so on
docker build ./ -t [Image name] #Create an image from Dockerfile
docker run -d --rm -p 8080:80 [Image name] #Start the image as a daemon
docker-compose
commandIn the directory where docker-compose.yml is located
docker-compose up -d #Up
docker-compose down #Make it stopped
docker-compose.yml
version: "3" #Specify version https://matsuand.github.io/docs.docker.jp.onthefly/compose/compose-file/compose-versioning/
services:
postgresql:
build: ./docker/postgresql/ #Where the Dockerfile is located
ports:
- 15432:5432 #Port mapping
environment:
POSTGRES_USER: hoge #Specifying environment variables
POSTGRES_PASSWORD: hogehoge
volumes:
- ./docker/postgresql/initdb.d:/docker-entrypoint-initdb.d #Volume mapping
- ./docker/postgresql/data:/var/lib/postgresql/data
service1:
image: service1 #Directly specify the image
ports:
- 8888:8888
volumes:
- ./app:/usr/src/server
Reference site: I tried to explain how to write docker-compose.yml --Qiita
Recommended Posts