Skip to content

Container

The Container resource allows you to create and manage Docker containers using Alchemy.

Usage

typescript
import * as docker from "alchemy/docker";

const myContainer = await docker.Container("my-container", {
  image: "nginx:latest",
  name: "web-server",
  ports: [{ external: 80, internal: 80 }],
  start: true
});

Properties

NameTypeRequiredDescription
imageRemoteImage | stringYesDocker image to use for the container
namestringNoName for the container
commandstring[]NoCommand to run in the container
environmentRecord<string, string>NoEnvironment variables for the container
ports{ external: number | string, internal: number | string, protocol?: "tcp" | "udp" }[]NoPort mappings from host to container
volumes{ hostPath: string, containerPath: string, readOnly?: boolean }[]NoVolume mappings from host paths to container paths
networks{ name: string, aliases?: string[] }[]NoNetworks to connect to
restart"no" | "always" | "on-failure" | "unless-stopped"NoRestart policy
removeOnExitbooleanNoWhether to remove the container when it exits
startbooleanNoStart the container after creation

Outputs

NameTypeDescription
idstringThe ID of the container
state"created" | "running" | "paused" | "stopped" | "exited"The current state of the container
createdAtnumberTime when the container was created

Example

typescript
import * as docker from "alchemy/docker";

// Create a Docker network
const network = await docker.Network("app-network", {
  name: "microservices-network"
});

// Pull the Redis image
const redisImage = await docker.RemoteImage("redis-image", {
  name: "redis",
  tag: "alpine"
});

// Run Redis container
const redis = await docker.Container("redis", {
  image: redisImage.imageRef,
  name: "redis",
  networks: [{ name: network.name }],
  start: true
});

// Run the application container
const app = await docker.Container("app", {
  image: "my-node-app:latest",
  name: "web-app",
  ports: [{ external: 3000, internal: 3000 }],
  networks: [{ name: network.name }],
  environment: {
    REDIS_HOST: "redis",
    NODE_ENV: "production"
  },
  volumes: [
    { hostPath: "./logs", containerPath: "/app/logs" }
  ],
  restart: "always",
  start: true
});