Reputation: 10953
I'm using docker-compose to create my development environment. I want to build a specific image, but I don't know how to set a name for that image.
wildfly:
build: /path/to/dir/Dockerfile
container_name: wildfly_server
ports:
- 9990:9990
- 80:8080
environment:
- MYSQL_HOST=mysql_server
- MONGO_HOST=mongo_server
- ELASTIC_HOST=elasticsearch_server
volumes:
- /Volumes/CaseSensitive/development/wildfly/deployments/:/opt/jboss/wildfly/standalone/deployments/
links:
- mysql:mysql_server
- mongo:mongo_server
- elasticsearch:elasticsearch_server
When I execute docker-compose
everything is ok, but I get a random name for the new image. Is it possible to set a name to the build image?
Upvotes: 447
Views: 552219
Reputation: 37318
On version 4.38.0, I suddenly get this error using the image
directive with a custom Dockerfile:
! app Warning failed to resolve reference "phpapp-8.2/app:latest": failed to do request: Head "https://phpapp-8.2/v2/app/manifests/latest": dialing phpapp-8.2:443 container via direct connection
In order to get rid of that warning, we need to tell Docker not to pull any image from the registry and we can do this by using pull_policy: never
directive:
services:
app:
build:
context: ../docker/app/8.2
dockerfile: Dockerfile
image: phpapp-8.2/app
pull_policy: never
Upvotes: 0
Reputation: 4792
For docker-compose version 2 file format, you can build and tag an image for one service and then use that same built image for another service.
For my case, I want to set up an elasticsearch cluster with 2 nodes, they both need to use the same image, but configured to run differently. I also want to build my own custom elasticsearch image from my own Dockerfile. So this is what I did (docker-compose.yml
):
version: '2'
services:
es-master:
build: ./elasticsearch
image: porter/elasticsearch
ports:
- "9200:9200"
container_name: es_master
es-node:
image: porter/elasticsearch
depends_on:
- es-master
ports:
- "9200"
command: elasticsearch --discovery.zen.ping.unicast.hosts=es_master
You can see that in the first service definition es-master
, I use the build
option to build an image from the Dockerfile in ./elasticsearch
. I tag the image with the name porter/elasticsearch
with the image
option.
Then, I reference this built image in the es-node
service definition with the image
option, and also use a depends_on
to make sure the other container es-master
is built and run first.
Setting the name constrains scaling.
Upvotes: 322
Reputation: 554
in docker-compose.yml, add image: "YOUR-DESIRED-NAME"
line in your service e.g. if your service name is wildfly
and you want to name your image as wildfly_img
wildfly: image: wildfly_img
Upvotes: 3
Reputation: 7832
If Jenkins is used the project-name can be defined using stackName = prefix
.
jenkins:
stackName = 'foo'
docker:
services:
bar:
So the name becomes foo_bar
.
Upvotes: 0
Reputation: 947
I did this:
version: '3.8' services: app: build: . depends_on: - postgres ports: - "8080:8080" volumes: - ./:/usr/src/app/ container_name: docker-compose-employees image: [Docker-Hub-Username]/docker-compose:latest
The docker-compose app runs with a container name: docker-compose-employees
And an image called [Docker-Hub-Username]/docker-compose:latest
Upvotes: 1
Reputation: 81
you can customize the image name to build & container name during docker-compose up for this, you need to mention like below in the 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
Upvotes: 5
Reputation: 8063
If you specify image as well as build, then Compose names the built image with the webapp and optional tag specified in image:
image: webapp:tag
in the docker-compose file, it looks like.
version: '3.9'
services:
node-ecom:
build: .
image: "webapp:tag"
container_name: node-ecom
ports:
- "4000:3000"
volumes:
- ./:/app:ro
- /app/node_modules
- /config/.env
env_file:
- ./config/.env
Upvotes: 1
Reputation: 37
sudo docker-compose -p <project_name> build
assigns project name
Note: '-p' option comes before 'build' in the command
Ref: https://docs.docker.com/compose/reference/
Upvotes: 3
Reputation: 4744
According to 3.9 version of Docker compose, you can use image: myapp:tag
to specify name and tag.
version: "3.9"
services:
webapp:
build:
context: .
dockerfile: Dockerfile
image: webapp:tag
Reference: https://docs.docker.com/compose/compose-file/compose-file-v3/
Upvotes: 45
Reputation: 4837
As per docker-compose 1.6.0:
You can now specify both a build and an image key if you're using the new file format.
docker-compose build
will build the image and tag it with the name you've specified, whiledocker-compose pull
will attempt to pull it.
So your docker-compose.yml
would be
version: '2'
services:
wildfly:
build: /path/to/dir/Dockerfile
image: wildfly_server
ports:
- 9990:9990
- 80:8080
To update docker-compose
sudo pip install -U docker-compose==1.6.0
Upvotes: 115
Reputation: 37658
The name of the image generated by docker-compose depends on the folder name by default but you can override it by using --project-name
argument:
$ docker-compose --project-name foo build bar
$ docker images foo_bar
Once docker-compose 1.6.0 is out, you may specify build:
and image:
to have an explicit image name (see arulraj.net's answer).
A third is to create an image from the container:
$ docker-compose up -d bar
$ docker commit $(docker-compose ps -q bar) foo_bar
$ docker-compose rm -f bar
Upvotes: 80
Reputation: 1892
Depending on your use case, you can use an image which has already been created and specify it's name in docker-compose
.
We have a production use case where our CI server builds a named Docker image. (docker build -t <specific_image_name> .
). Once the named image is specified, our docker-compose
always builds off of the specific image. This allows a couple of different possibilities:
1- You can ensure that where ever you run your docker-compose
from, you will always be using the latest version of that specific image.
2- You can specify multiple named images in your docker-compose
file and let them be auto-wired through the previous build step.
So, if your image is already built, you can name the image with docker-compose
. Remove build
and specify image:
wildfly:
image: my_custom_wildfly_image
container_name: wildfly_server
ports:
- 9990:9990
- 80:8080
environment:
- MYSQL_HOST=mysql_server
- MONGO_HOST=mongo_server
- ELASTIC_HOST=elasticsearch_server
volumes:
- /Volumes/CaseSensitive/development/wildfly/deployments/:/opt/jboss/wildfly/standalone/deployments/
links:
- mysql:mysql_server
- mongo:mongo_server
- elasticsearch:elasticsearch_server
Upvotes: 33
Reputation: 3416
after you build your image do the following:
docker tag <image id> mynewtag:version
after that you will see your image is no longer named <none>
when you go docker images
.
Upvotes: 14