Wednesday, 27 April 2022

Docker Compose 101

*********

# Build images specified in DockerFile for services to be used in a container (container is VM like)

# No images and containers will actually be created

docker-compose build


# Run one off command against a service specifed in yml, no container will be created at this moment

# For example running a shell command sh -c to a service 

docker-compose run <serviceName> sh -c "..."


# Run services in yml, and builds images specified in dockerFile for services 

and create a contianer. if cmds in service will be run as 

docker-compose up 


# specify list of services

docker-compose ps 



**Need a dockerfile to build image for by docker or build image for services in the container by docker-compose

DockerFile ;

From a docker image from docker hub

ENV (env variables)

RUN mkdir /app

# Set workdir

WORKDIR /app

# copy existing dir to docker image

COPY ./app /app


# add user without home directory to just run docker

RUN adduser -D appuser

# switch to thsis user

USER appuser


// Builds a docker image (Usually not used if using docker-compose)

 docker build .


// 

docker-compose.yml

specify 

services to be created in container 

including ports to be mapped from machine to docker container

volumes to be mapped(****This is important because you want the folder in your machine to be mapped to docker container so when you run docker-compose up or docker-compose run the folders are synced with each other)


(***for ubuntu, linux if not configured, docker compose volume will be created by root user, this is not good because your cerated user in compose file wont be able to run programs in the directory)


for solution refer to another blog


command to be run when container is created for the service and running the service


...

# > means commands to be entered in next line and start with 1 indent from commands

commands: >

No comments:

Post a Comment