COPY
is used to copy contents from the local directory to the container.- This is necessary as without it the container will not have any files. Files in
.dockerignore are not copied
- Copy everything to the home container directory:
COPY . .
WORKDIR
is similar to cd and is used to change the working directory inside the container.- Some commands like
RUN python ...
do not accept relative imports and may require multipleWORKDIR
commands to change the path. - When using multiple
WORKDIR
commands the changes to the directory location should be relative to the previousWORKDIR
EXPOSE
exposes the specified port and makes it available only for inter-container communicationEXPOSE
does not make the ports of the container accessible to the host
Docker Compose is used to make it easy to build and run multiple container with using a single cofiguration file called docker-compose.yml
.
version: "3" #This is the version of the docker-composr file format and not the docker-compose tool itself
services:
#Service without build
elasticsearch: #This is the service name and all the properties under this will be used to build a container for this service
image: "docker.elastic.co/elasticsearch/elasticsearch:7.8.0" #The name of the pre-built image to be pulled and used
container_name: elasticsearch #Name of the container created
environment: #Environment variables can be specifoed here (specified with -e in the docker build command)
- discovery.type=single-node
ports: #Port mapping with the format <host_port>:<container_port>. The container port will be mapped to the host port. This is necessary to access a service like an API from our host.
- 5000:80 #Here the service is running on port 80 on the container and any request received by the local port 5000 will be passed to port 80 on the container
- 9200:9200
- 9300:9300
#Service with build
chatbot-api:
build: . #Specify the relative path to the directory containing the Dockerfile to build this container image.
image: chatbot-backend:latest
container_name: chatbot-backend
volumes: #Map volume from host to container for use cases like hot reloading.
- ./:/app
ports:
- 8112:8112
host.docker.internal
resolves to the internal IP address used by the host on windows and Mac172.17.0.1
should be used for Linux
version: '3.7'
services:
app:
image: your-app:latest
ports:
- "8080:8080"
environment:
DB_UPSTREAM: http://${DOCKER_GATEWAY_HOST:-host.docker.internal}:3000
-
The
DB_UPSTREAM
should point to the host's IP and port 3000.${DOCKER_GATEWAY_HOST:-host.docker.internal}
is the critical piece here. This expression tells docker-compose to either us the environment variableDOCKER_GATEWAY_HOST
or use the fallbackhost.docker.internal
when resolving this value. -
This works out of the box now without anything left to do.
-
If you are running this stack on Linux you need to have the DOCKER_GATEWAY_HOST environment variable set for the Docker gateway host. Simply put this line into your .bashrc (.bash_profile or .zshrc):
export DOCKER_GATEWAY_HOST=172.17.0.1
- Get container id by listing all the running containers with
docker ps
- Run
docker exec -it <container_id> /bin/bash
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu `lsb_release -cs` test"
sudo apt update
sudo apt install docker-ce
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version #If you get an error when running docker-compose up, try doing it with sudo
docker rmi $(docker images --format '{{.Repository}}:{{.Tag}}' | grep 'zulh')
Article Link - https://drive.google.com/file/d/1I0pxCgSgKKfz_MO_x24m3alTfJNSJE5D/view?usp=sharing