问题
I have a few docker containers that I try to sync with docker-compose
(currently are being run by bash scripts). I'm looking for a way to tag and push them to our ec2 based dockerhub (private server).
Using simply docker
we did something like this (for each container):
$ docker build -f someDockerfile -t some
$ docker tag some <docker_hub_server>/some
$ docker push <docker_hub_server>/some
I'm trying to replicate this in docker-compose
. I tried a few things but this one seems close to working (but didn't work of course):
docker-compose.yml:
version: '3'
services:
some:
container_name:some
image: some:<docker_hub_server>/some
But when i run:
$ docker-compose push
I get:
Pushing some (base:<docker_hub_server>/base:latest)...
ERROR: invalid reference format
Any ideas on how I can tag and push my containers?
p.s.: I know that this is not the way docker-compose
is meant to be used, but I also know it's possible and it fits my needs.
回答1:
I have tested this approach with Docker Hub, so you should be able to achieve what you want with the following configuration and shell session:
docker-compose.yml
version: '3'
services:
build-1:
build:
context: ./build-1
image: user/project-1
build-2:
build:
context: ./build-2
image: user/project-2
(Here, you should replace user/project-1
with registry.name/user/project-1
if you are not using Docker Hub but another Docker registry, e.g., quay.io/user/project-1
.)
The various fields involved here (build:
, context:
, etc.) are described in this page of the docker-compose documentation.
The docker-compose.yml
file above assume you have the following tree (including a .gitignore and some .dockerignore files, to comply with best practices):
.
├── build-1
│ ├── Dockerfile
│ └── .dockerignore
├── build-2
│ ├── Dockerfile
│ └── .dockerignore
├── docker-compose.yml
└── .gitignore
Then do in a terminal:
$ docker login
# → append the domain name of your Docker registry
# if you are not using Docker Hub; for example:
# docker login quay.io
$ docker-compose build --pull
$ docker-compose push
# and optionally:
$ docker logout
Finally, below are some remarks to clarify a few details related to your question:
In your example session
$ docker build -f someDockerfile -t some . # with "." as context build path $ docker tag some …/some $ docker push …/some
some
is a temporary image name (not a container) so it seems unnecessary: you could just as well have run the following, with the same outcome.$ docker build -f someDockerfile -t …/some . $ docker push …/some
Your
docker-compose.yml
example contained the line:image: some:<docker_hub_server>/some
Actually, the image tags can contain
:
to specify a version, but not in this way (it should be a suffix). For example, you could tag an imageuser/some:1.0
oruser/some:latest
, and by convention this latter exampleuser/some:latest
admitsuser/some
as a shorter, equivalent name.Note that the full syntax for image tags is
registry.name:port/user/project:version
where
registry.name
should be the domain name or hostname of the desired Docker registry (if omitted, it will default to Docker Hub).This is mentioned in that page of the official documentation.
So for example, if you use the Quay Docker registry, the image tag could be
quay.io/user/some:latest
or more succinctlyquay.io/user/some
.
来源:https://stackoverflow.com/questions/53416685/docker-compose-tagging-and-pushing