I'm trying to connect two containers with a docker-compose-yml, but it isn't working. This is my docker-compose.yml file:
version: "3"
services:
datapower:
build: .
ports:
- "9090:9090"
depends_on:
- db
db:
image: "microsoft/mssql-server-linux:2017-latest"
environment:
SA_PASSWORD: "your_password"
ACCEPT_EULA: "Y"
ports:
- "1433:1433"
When I make:
docker-compose up
This up my two containers. Then I stop one container and then I run the same container stoped independiently like:
docker-compose run -u root --name nameofcontainer 'name of container named in docker-compose.yml'
With this, the connection of the containers works. Exists a method to configure my docker-compose.yml to connect my containers like root without stop a container and run independently?
Update:
There exists the user property that can be set in the compose file. This is documented in docker-compose file reference.
...
services:
datapower:
build: .
user: root
ports:
- "9090:9090"
depends_on:
- db
...
Setting both a User AND a Group in docker-compose.yml:
Discovered another way to set not only the user but also the group in a docker-compose.yml file which is NOT documented in the Docker Compose File Reference #yamenk helpfully provides in the accepted answer.
I needed to raise a container expressly setting both a user AND a group and found that the user: parameter in docker-compose.yml can be populated as a UID:GID mapping delimited by a colon.
Below is a snippet from my docker-compose.yml file where this form was tested and found to work correctly:
services:
zabbix-agent:
image: zabbix/zabbix-agent2:ubuntu-6.0-latest
container_name: DockerHost1-zabbix-agent2
user: 0:0
<SNIP>
Reference:
https://github.com/zabbix/zabbix-docker/issues/710
Hope this saves others wasted cycles looking for this!
Related
I have two django apps. Both are run as part of two different docker-compose files.
App 1 docker-compose.yml file:
services:
django:
build: .
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
ports:
- "8013:8000"
volumes:
- ./:/app
depends_on:
- db
App 2 docker-compose.yml file
services:
django:
build: .
container_name: "web"
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
ports:
- "8003:8000"
volumes:
- ./:/app
depends_on:
- db
So basically, my goal is to call App 2's django endpoint from App 1. To do this, in app 1's code, I utilize url http://web:8003/app2_endpoint
Also, I have ALLOWED_HOSTS=['*'] in both projects
Yet, I end up with Max retries exceeded error.
I also came across this question, but I failed to figure out the solution for my case.
If you don't specify custom docker network in your compose file, each compose file would create a separated network for itself. So basically your containers in separated compose can't see each other
The solution can be using same docker network in compose files. Sth like:
services:
...
networks:
default:
external: true
name: YOUR_DOCKER_NETWORK
And add it in another compose too
This tells compose to use an external docker network as default, named YOUR_DOCKER_NETWORK
Note that you should create this network by yourself, because it's external:
docker network create YOUR_DOCKER_NETWORK
You can also use custom networks
Docs in https://docs.docker.com/compose/networking/
What is the use of container_name in docker-compose.yml file? Can I use it as hostname which is nothing but the service name in docker-compose.yml file.
Also when I explicitly write hostname under services does it override the hostname represented by service name?
hostname: just sets what the container believes its own hostname is. In the unusual event you got a shell inside the container, it might show up in the prompt. It has no effect on anything outside, and there’s usually no point in setting it. (It has basically the same effect as hostname(1): that command doesn’t cause anything outside your host to know the name you set.)
container_name: sets the actual name of the container when it runs, rather than letting Docker Compose generate it. If this name is different from the name of the block in services:, both names will be usable as DNS names for inter-container communication. Unless you need to use docker to manage a container that Compose started, you usually don’t need to set this either.
If you omit both of these settings, one container can reach another (provided they’re in the same Docker Compose file and have compatible networks: settings) using the name of the services: block and the port the service inside the container is listening in.
version: '3'
services:
redis:
image: redis
db:
image: mysql
ports: [6033:3306]
app:
build: .
ports: [12345:8990]
env:
REDIS_HOST: redis
REDIS_PORT: 6379
MYSQL_HOST: db
MYSQL_PORT: 3306
The easiest answer is the following:
container_name: This is the container name that you see from the host machine when listing the running containers with the docker container ls command.
hostname: The hostname of the container. Actually, the name that you define here is going to the /etc/hosts file:
$ exec -it myserver /bin/bash
bash-4.2# cat /etc/hosts
127.0.0.1 localhost
172.18.0.2 myserver
That means you can ping machines by that names within a Docker network.
I highly suggest set these two parameters the same to avoid confusion.
An example docker-compose.yml file:
version: '3'
services:
database-server:
image: ...
container_name: database-server
hostname: database-server
ports:
- "xxxx:yyyy"
web-server:
image: ...
container_name: web-server
hostname: web-server
ports:
- "xxxx:xxxx"
- "5101:4001" # debug port
you can customize the image name to build & container name during docker-compose up for this, you need to mention like below in docker-compose.yml file.
It will create an image & container with custom names.
version: '3'
services:
frontend_dev:
stdin_open: true
environment:
- CHOKIDAR_USEPOLLING=true
build:
context: .
dockerfile: Dockerfile.dev
image: "mycustomname/sample:v1"
container_name: mycustomname_sample_v1
ports:
- '3000:3000'
volumes:
- /app/node_modules
- .:/app
I have a docker-compose.yml which get's 2 services up (I have left out all irrelevant data from it).
app:
build:
context: .
dockerfile: ./docker/app/Dockerfile
image: ...
container_name: app-${ENV}
depends_on:
- db
expose:
- 80
db:
image: ...
container_name: my-cool-db
ports:
- "3306:3306"
The point to see here is that app is getting a container name depending on the parameter. So basically I have a shell script running the following:
ENV=$1 docker-compose -p $1 up -d
So in short, whatever I forward as parameter, the new app container should be brought up. For example if I do sh initializer.sh first I will get app-first container. -p parameter is specified so I can have multiple instances of same container classified as a different project.
If I have a single container this works great, and I end up with say:
app-first
app-second
app-third
What I would like to achieve is to have all containers use the same DB. But when I do a docker-compose my DB container still wants to be brought up independent of his existence already.
Is it an issue that it tries to create a DB under different project name, but with same container name so it causes the collision?
Can this be made without bringing up 2 separate DB containers?
A hacky solution:
change your compose file to
services:
app:
image: ...
container_name: app-${ENV}
networks:
- shared
expose:
- 80
db:
image: ...
container_name: my-cool-db
networks:
- shared
ports:
- "3306:3306"
networks:
shared:
external: true
Then first create the network docker network create shared
Bring up db: docker-compose up -d db
First app: ENV=first docker-compose -p first up -d app
Second app: ENV=second docker-compose -p second up -d app
docker-composeA.yml
mysql:
image: mysql
environment:
- XXX=XXX
gogs:
image: gogs/gogs
links:
- mysql:mysql # ok
docker-composeB.yml
tomcat:
image: javaweb:8
links:
- mysql:mysql // wrong, can not find mysql defination
Now I want to link mysql container which defined in docker-composeA.yml, but when I run docker-compose up with docker-composeB.yml, it said 'mysql is undefined'. So How could I link container cross docker-compose.yml files.
links and depends_on both require a reference to a service defined in the same Docker Compose file. You need external_links.
I want to have two docker-compose files, where one overrides another.
(The motivation comes from Docker Compose Docs)
The use case comes from the buildbot environment. The first docker-compose file should define a simple service. This is a service that is going to be tested. Let's take
version: '2'
services:
service-node:
build:
context: ./res
dockerfile: Dockerfile
image: my/server
env_file: .env
The second docker-compose file (let's name it docker-compose.test.yml) overrides the service-node to add a buildbot worker feature, and creates the second container, i.e. buildbot master node, that is going to control testing machinery. Let's take
version: '2'
services:
service-node:
build:
context: ./res
dockerfile: buildbot.worker.Dockerfile
image: my/buildbot-worker
container_name: bb-worker
env_file: ./res/buildbot.worker.env
environment:
- BB_RES_DIR=/var/lib/buildbot
networks:
testlab:
aliases:
- bb-worker
volumes:
- ./vol/bldbot/worker:/home/bldbotworker
depends_on:
- bb-master
bb-master:
build:
context: ./res
dockerfile: buildbot.master.Dockerfile
image: my/buildbot-master
container_name: bb-master
env_file: ./res/buildbot.master.env
environment:
- BB_RES_DIR=/var/lib/buildbot
networks:
- testlab
expose:
- "9989"
volumes:
- ./vol/bldbot/master:/var/lib/buildbot
networks:
testlab:
driver: bridge
Generally this configuration works, i.e. the command
docker-compose -f docker-compose.yml -f docker-compose.test.yml up -d
builds both images and runs both containers, but there is one shortcoming, i.e. the command
docker-compose ps
shows only one service, bb-worker. At the same time
docker ps
shows both.
Furthermore, the command
docker-compose down
stops only one service, and outputs the message/warning Found orphan containers. Of course, the message refers to bb-master.
How can I override the basic docker-compose.yml file to be able to add additional non-orphan service?
You need to run all docker-compose commands with the flags, e.g.:
docker-compose -f docker-compose.yml -f docker-compose.test.yml down
Alternatively, you can make this the default by writing the following to a .env file in the same folder:
COMPOSE_FILE=docker-compose.yml:docker-compose.test.yml
NOTE:
In windows you need tu use ";" as the separator (#louisvno)