Docker Swarm - Using mongo service only in local enviroment - docker

So i learned that you can use the same docker-compose file for both local development with docker-machine and for production environment with docker swarm, which is great think, you can also share the same docker-compose file and add specific configuration for production/local enviroment.
One think i couldnt find is a way to define a service only on local enviroment, i have compose file locally which contain redis/node/nginx/mongo containers BUT in production i dont want to use docker for mongo since i use external server(Atlas/ MLab), is it possible to do so and keep using the same files for production & development ?

Docker compose has a solution for the different environment compose files Multiple Compose files. You basically extract the differences into a sub compose file and when running you can merge the compose files:
docker-compose.yml
version: '3'
services:
redis:
...
node:
...
nginx:
...
docker-compose.local.yml
...
services:
mongo:
...
When running locally you execute:
docker-compose -f docker-compose.yml -f docker-compose.local.yml up -d
And when deploying to Swarm you just execute:
docker stack deploy --compose-file docker-compose.yml

Related

deploy on Heroku a docker image (with volumes), without having source code

I need to deploy on Heroku a Docker image I have from a public registry.
Such image needs some params to be run, including a certificate file.
Locally, I can use docker compose specifying in the docker-compose.yml file all the env vars and volumes.
# === docker-compose.yml ===
services:
my_service:
image: public.images.repo/my-service
container_name: my_service
volumes:
- /Users/me/public.pem:/public.pem
environment:
- CERT_PATH=/public.pem
Unfortunately, I've just seen that Heroku doesn't support docker compose.
I see that is supports the heroku.yml file, but it requires to have the Dockerfile, that I don't have and can't modify since I only have the image. And, apparently, there is no volume field.
# === heroku.yml ===
build:
docker:
web: Dockerfile
worker: worker/Dockerfile
release:
image: worker
How can I deploy a docker container, with volumes to import certificate files?
Heroku does not support Docker volumes at all:
Unsupported Dockerfile commands
VOLUME - Volume mounting is not supported. The filesystem of the dyno is ephemeral.
You could create a custom image based on the public image that gets the certificate file some other way.
Without more information about the container you're trying to run it's hard to say much more.

Create multiple docker containers by running same docker-compose config

I'm using docker-compose to start a job on a remote server:
version: "3.9"
services:
solution-app:
container_name: solution_new_name_v3
build: .
I ran the following command to deploy the container:
docker-compose -H "ssh://..." up -d --build
This works great, now I wanted to start another job in parallel using the same docker compose file, but with some minor changes in the source code. So I changed the container_name in the compose config and assumed that it should start a new container. However, instead of starting a new container my previous container was stopped and replaced with a new one with this new name.
So my question is: how can I start few containers running in parallel using same docker-compose config?
I figure out the way:
so Docker compose maps all services based on the project name, which is by default a directory name. To override the project name one can use:
docker-compose -p NAME
Alternatively one can set COMPOSE_PROJECT_NAME env variable

How to `--scale` from within docker-compose file

Let's say I have the following docker-compose.yml file:
version: '3'
services:
load-balancer:
...
...
web-application:
...
...
If I want to run this with 5 replicas of web-application, I have to issue this command:
docker-compose up --scale web-application=5
Is there any way to tell Docker to do the --scale web-application=5 bit from within the docker-compose.yml file?
You can specify the number of replicas in docker-compose only when working in swarm mode.
See the deploy directive.
Example:
services:
redis:
image: redis:latest
deploy:
replicas: 2
From the documentation:
This only takes effect when deploying to a swarm with
docker stack deploy, and is ignored by docker-compose up
and docker-compose run.
Lastly, there is some discussion about this feature (since it used to be possible) in this GitHub issue, and here is the mention in the most recent compose spec.
I tested replicas in docker compose file, the code that worked for me is the following.
You should use docker-compose up -d to execute the configuraciĆ³n
You should see the next results.
The results in web browser should be the following:

Start particular service from docker-compose

I am new to Docker and have docker-compose.yml which is containing many services and iI need to start one particular service. I have docker-compose.yml file with information:
version: '2'
services:
postgres:
image: ${ARTIFACTORY_URL}/datahub/postgres:${BUILD_NUMBER}
restart: "no"
volumes:
- /etc/passwd:/etc/passwd
volumes_from:
- libs
depends_on:
- libs
setup:
image: ${ARTIFACTORY_URL}/setup:${B_N}
restart: "no"
volumes:
- ${HOME}:/usr/local/
I am able to call docker-compose.yml file using command:
docker-compose -f docker-compose.yml up -d --no-build
But I need to start "setup service" in docker-compose file:
How can I do this?
It's very easy:
docker compose up <service-name>
In your case:
docker compose -f docker-compose.yml up setup -d
To stop the service, then you don't need to specify the service name:
docker compose down
will do.
Little side note: if you are in the directory where the docker-compose.yml file is located, then docker-compose will use it implicitly, there's no need to add it as a parameter.
You need to provide it in the following situations:
the file is not in your current directory
the file name is different from the default one, eg. myconfig.yml
As far as I understand your question, you have multiple services in docker-compose but want to deploy only one.
docker-compose should be used for multi-container Docker applications. From official docs :
Compose is a tool for defining and running multi-container Docker
applications.
IMHO, you should run your service image separately with docker run command.
PS: If you are asking about recreating only the container whose image is changed among the multiple services in your docker-compose file, then docker-compose handles that for you.

How to sync code between container and host using docker-compose?

Until now, I have used a local LAMP stack to develop my web projects and deploy them manually to the server. For the next project I want to use docker and docker-compose to create a mariaDB, NGINX and a project container for easy developing and deploying.
When developing I want my code directory on the host machine to be synchronised with the docker container. I know that could be achieved by running
docker run -dt --name containerName -v /path/on/host:/path/in/container
in the cli as stated here, but I want to do that within a docker-compose v2 file.
I am as far as having a docker-composer.yml file looking like this:
version: '2'
services:
db:
#[...]
myProj:
build: ./myProj
image: myProj
depends_on:
- db
volumes:
myCodeVolume:/var/www
volumes:
myCodeVolume:
How can I synchronise my /var/www directory in the container with my host machine (Ubuntu desktop, macos or Windows machine)?
Thank you for your help.
It is pretty much the same way, you do the host:container mapping directly under the services.myProj.volumes key in your compose file:
version: '2'
services:
...
myProj:
...
volumes:
/path/to/file/on/host:/var/www
Note that the top-level volumes key is removed.
This file could be translated into:
docker create --links db -v /path/to/file/on/host:/var/www myProj
When docker-compose finds the top-level volumes section it tries to docker volume create the keys under it first before creating any other container. Those volumes could be then used to hold the data you want to be persistent across containers.
So, if I take your file for an example, it would translate into something like this:
docker volume create myCodeVolume
docker create --links db -v myCodeVoume:/var/www myProj

Resources